﻿Imports TBCC.UtilityMobile




Public Class HDataTypeService

    Private _pi As ProjectItem
    Private pid_str As String
    Private netid As Integer

    Public Sub New(ByVal pi As ProjectItem)
        _pi = pi
        netid = 1
        pid_str = Trim(pi.ProjectID).Replace("-", "_")
    End Sub

    Public Function GetHDataGroupList(ByVal ParentID As Integer) As IList(Of HDataTypeItem)

        Return GetHDataGroupList(ParentID, Nothing, Nothing)

    End Function

    Public Function GetHDataGroupList(ByVal ParentID As Integer, ByVal BeginTime As DateTime, ByVal EndTime As DateTime) As IList(Of HDataTypeItem)
        Dim il As IList(Of HDataTypeItem)
        il = New List(Of HDataTypeItem)

        Dim ds As DataSet

        Dim strSql As String
        strSql = String.Format("SELECT * FROM TbccHDData_{0}_{1} WHERE 1=1 ", pid_str, netid)

        strSql &= "AND "
        If ParentID <> -1 Then
            strSql &= String.Format("ParentID='{0}' ", ParentID)
        Else
            strSql &= "1 "
        End If

        strSql &= "AND "
        If BeginTime <> Nothing Then
            strSql &= String.Format("RecordTime>='{0}' ", UtilityClass.GetDateString(BeginTime)) '能显示=开头值
        Else
            strSql &= "1 "
        End If

        strSql &= "AND "
        If EndTime <> Nothing Then
            strSql &= String.Format("RecordTime<='{0}' ", UtilityClass.GetDateString(EndTime.AddSeconds(1))) '不能显示=末尾值; 解决:将末尾时间加1秒
        Else
            strSql &= "1 "
        End If

        ds = TBCC.UtilityMobile.SqlHelper.ExecuteDataset(strSql, Nothing)

        Dim seqNo As Integer = 1
        Dim isPrjTypeCar As Boolean : isPrjTypeCar = (_pi.ProjectType = ProjectType_Enum.Car)
        For Each r As DataRow In ds.Tables(0).Rows
            Dim hdti As New HDataTypeItem()
            hdti.SeqNo = seqNo
            hdti.ParentID = r("ParentID")
            hdti.NetID = r("NetID")
            hdti.RecordTime = r("RecordTime")
            hdti.AI1 = r("AI1")
            hdti.FormatAI1 = Program.ConvertFormatAI(r("AI1"))
            If isPrjTypeCar Then
                hdti.AI2 = r("AI2")
                hdti.AI3 = r("AI3")
                hdti.AI4 = r("AI4")
                hdti.FormatAI2 = Program.ConvertFormatAI(r("AI2"))
                hdti.FormatAI3 = Program.ConvertFormatAI(r("AI3"))
                hdti.FormatAI4 = Program.ConvertFormatAI(r("AI4"))
            End If

            hdti.Longitude_Dir = r("Longitude_Dir")
            hdti.Longitude = r("Longitude")
            hdti.FormatLongitude = Program.Gps_GetFormatLongitude(r("Longitude"), r("Longitude_Dir"))

            hdti.Latitude_Dir = r("Latitude_Dir")
            hdti.Latitude = r("Latitude")
            hdti.FormatLatitude = Program.Gps_GetFormatLatitude(r("Latitude"), r("Latitude_Dir"))

            seqNo += 1
            il.Add(hdti)
        Next

        Return il
    End Function


    Public Function FillHDataGroupList(ByVal il As IList(Of HDataTypeItem), ByVal ParentID As Integer) As Boolean

        Return FillHDataGroupList(il, ParentID, Nothing, Nothing)

    End Function

    Public Function FillHDataGroupList(ByVal il As IList(Of HDataTypeItem), ByVal ParentID As Integer, ByVal BeginTime As DateTime, ByVal EndTime As DateTime) As Boolean

        If il Is Nothing Then Return False

        Dim ds As DataSet

        Dim strSql As String
        strSql = String.Format("SELECT * FROM TbccHDData_{0}_{1} WHERE 1=1 ", pid_str, netid)

        strSql &= "AND "
        If ParentID <> -1 Then
            strSql &= String.Format("ParentID='{0}' ", ParentID)
        Else
            strSql &= "1 "
        End If

        strSql &= "AND "
        If BeginTime <> Nothing Then
            strSql &= String.Format("RecordTime>='{0}' ", UtilityClass.GetDateString(BeginTime)) '能显示=开头值
        Else
            strSql &= "1 "
        End If

        strSql &= "AND "
        If EndTime <> Nothing Then
            strSql &= String.Format("RecordTime<='{0}' ", UtilityClass.GetDateString(EndTime.AddSeconds(1))) '不能显示=末尾值; 解决:将末尾时间加1秒
        Else
            strSql &= "1 "
        End If

        ds = TBCC.UtilityMobile.SqlHelper.ExecuteDataset(strSql, Nothing)

        Dim seqNo As Integer = 1
        Dim isPrjTypeCar As Boolean : isPrjTypeCar = (_pi.ProjectType = ProjectType_Enum.Car)
        For Each r As DataRow In ds.Tables(0).Rows
            Dim hdti As New HDataTypeItem()
            hdti.SeqNo = seqNo
            hdti.ParentID = r("ParentID")
            hdti.NetID = r("NetID")
            hdti.RecordTime = r("RecordTime")
            hdti.AI1 = r("AI1")
            If isPrjTypeCar Then
                hdti.AI2 = r("AI2")
                hdti.AI3 = r("AI3")
                hdti.AI4 = r("AI4")
            End If
            hdti.Longitude_Dir = r("Longitude_Dir")
            hdti.Longitude = r("Longitude")
            hdti.Latitude_Dir = r("Latitude_Dir")
            hdti.Latitude = r("Latitude")

            seqNo += 1
            il.Add(hdti)
        Next

        Return True
    End Function

    Public Function InsertHDataTypeItem(ByVal hdti As HDataTypeItem) As Boolean

        Dim strSql As String = String.Empty
        Dim p() As MySql.Data.MySqlClient.MySqlParameter = Nothing

        Select Case _pi.ProjectType
            Case ProjectType_Enum.Box

                strSql = String.Format("INSERT INTO TbccHDData_{0}_{1}(ParentID,NetID,RecordTime,AI1,Longitude_Dir,Longitude,Latitude_Dir,Latitude) " _
                                    & "VALUES(?p0,?p1,?p2,?p3,?p4,?p5,?p6,?p7)", pid_str, netid)

                ReDim p(7)
                For i As Integer = 0 To p.Length - 1
                    p(i) = New MySql.Data.MySqlClient.MySqlParameter()
                    p(i).ParameterName = "p" + i.ToString
                Next

                p(0).Value = hdti.ParentID
                p(1).Value = hdti.NetID
                p(2).Value = hdti.RecordTime
                p(3).Value = hdti.AI1
                p(4).Value = hdti.Longitude_Dir
                p(5).Value = hdti.Longitude
                p(6).Value = hdti.Latitude_Dir
                p(7).Value = hdti.Latitude

            Case ProjectType_Enum.Car

                strSql = String.Format("INSERT INTO TbccHDData_{0}_{1}(ParentID,NetID,RecordTime,AI1,AI2,AI3,AI4,Longitude_Dir,Longitude,Latitude_Dir,Latitude) " _
                                    & "VALUES(?p0,?p1,?p2,?p3,?p4,?p5,?p6,?p7,?p8,?p9,?p10)", pid_str, netid)

                ReDim p(10)
                For i As Integer = 0 To p.Length - 1
                    p(i) = New MySql.Data.MySqlClient.MySqlParameter()
                    p(i).ParameterName = "p" + i.ToString
                Next

                p(0).Value = hdti.ParentID
                p(1).Value = hdti.NetID
                p(2).Value = hdti.RecordTime
                p(3).Value = hdti.AI1
                p(4).Value = hdti.AI2
                p(5).Value = hdti.AI3
                p(6).Value = hdti.AI4
                p(7).Value = hdti.Longitude_Dir
                p(8).Value = hdti.Longitude
                p(9).Value = hdti.Latitude_Dir
                p(10).Value = hdti.Latitude

        End Select

        Return SqlHelper.ExecuteNonQuery(strSql, p)

    End Function

    Public Function DeleteHDataTypeItem(ByVal beginTime As DateTime, ByVal endTime As DateTime) As Boolean
        Dim tic As New TBCC.UtilityMobile.TranstrationItem()
        Dim strSql As String
        Dim index As Integer

        strSql = String.Format("DELETE FROM TbccHDData_{0}_{1} Where RecordTime>='{2}' AND RecordTime<='{3}' -- " & index, _
                                pid_str, netid, UtilityClass.GetDateString(beginTime), UtilityClass.GetDateString(endTime))
        tic.Add(strSql, Nothing)
        index += 1

        Return SqlHelper.ExecuteTranstration(tic)
    End Function

    Public Function DeleteHDataTypeItem(ByVal parentID As Integer) As Boolean
        Dim tic As New TBCC.UtilityMobile.TranstrationItem()
        Dim strSql As String
        Dim index As Integer

        strSql = String.Format("DELETE FROM TbccHDData_{0}_{1} Where ParentID='{2}' -- " & index, _
                                pid_str, netid, parentID)
        tic.Add(strSql, Nothing)
        index += 1

        Return SqlHelper.ExecuteTranstration(tic)
    End Function

End Class
